// ========================= 全屏 =========================
// 判断当前是否是全屏
export const isFullscreen = () => {
    return document.fullscreenElement ||
        document.msFullscreenElement  ||
        document.mozFullScreenElement ||
        document.webkitFullscreenElement || false;
};

// 全屏
export const launchFullscreen = () => {
    const docElm = document.documentElement;
    if(docElm.requestFullscreen) {
        docElm.requestFullscreen();
    } else if(docElm.mozRequestFullScreen) {
        docElm.mozRequestFullScreen();
    } else if(docElm.webkitRequestFullScreen) {
        docElm.webkitRequestFullScreen();
    } else if(Boolean(docElm.msRequestFullscreen)) {
        docElm.msRequestFullscreen();
    }
};

// 取消全屏
export const exitFullscreen = () => {
    if(document.exitFullscreen) {
        document.exitFullscreen();
    } else if(document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
    } else if(document.webkitCancelFullScreen) {
        document.webkitCancelFullScreen();
    } else if(document.msExitFullscreen) {
        document.msExitFullscreen();
    }
};

// ========================= 深拷贝-递归方式 =========================
export const deepClone = (obj) => {
    let objClone = Array.isArray(obj) ? [] : {};
    if(obj && typeof obj === 'object') {
        for(let key in obj) {
            if(obj.hasOwnProperty(key)) {
                //判断ojb子元素是否为对象，如果是，递归复制
                if(obj[key] && typeof obj[key] === 'object') {
                    objClone[key] = deepClone(obj[key]);
                }else {
                    //如果不是，简单复制
                    objClone[key] = obj[key];
                }
            }
        }
    }
    return objClone;
};

// ========================= 树形结构 =========================
/**
 * @param data - 数据
 * @param idField - 标识字段名称
 * @param parentIdField - 父标识字段名称
 * @param childrenField - 子节点字段名称
 * @param isFilter - 是否进行过滤（停用）
 * @param isDisabled - 是否禁用
 * 
*/
export const setTreeData = (
    data, 
    idField, 
    parentIdField, 
    childrenField, 
    isFilter = true,
    isDisabled = false) => {
    let map = {};
    let result = [];
    let cloneData = deepClone(data);
    cloneData.forEach(el => {
        // 建立每个数组元素id和该对象的关系
        map[el[idField]] = el;
    });
    cloneData.forEach((item, index) => {
        isDisabled && (item.disabled = true);
        // 每一项item 的父亲数据
        let parent = map[item[parentIdField]];
        if(parent) {
            // 当前item 有父亲，就把item 放在父亲元素的children 下面
            // if(Array.isArray(parent.children)) {
            //     parent.children.push(item);
            // }else {
            //     parent.children = [];
            //     parent.children.push(item);
            // }
            if(isFilter && item.state) {
                (parent[childrenField] || (parent[childrenField] = [])).push(item);
            };
            !isFilter && (parent[childrenField] || (parent[childrenField] = [])).push(item);
        }else {
            // 元素没有父亲，是根节点，把节点push到最终结果中
            if(isFilter && item.state) {
                result.push(item);
            };
            !isFilter && result.push(item);
        };
    });
    
    return result;
};

// ========================= 数据操作 =========================
// 判断对象为空
export const isEmptyObject = (obj) => {
    return Object.keys(obj).length === 0;
};

// 获取对象数组中的某个属性最大值
export const objectMaxAttribute = (data, maxField) => {
    return data.map(obj => obj[maxField]).sort().pop();
};

// 拼接对象数组的某属性值
export const montageStr = (objs, field, isReturnStr = false) => {
    let result = objs.map((item, index) => {
        return item[field];
    });
    if(isReturnStr) return result.join(',');
    return result;
};

// 对象数组去除重复项
export const mergeAndUniqueObjects = (data, field) => {
    return data.filter((item, index) =>
    data.findIndex(i => i[field] === item[field]) === index);
};

// 对象数组是否存在此对象
export const findObjects = (objs, data, field) => {
    return objs.find(item => item[field] === data[field]);
};

// 查找对象在数组中的下标
export const findIndexObjects = (objs, data, field) => {
    return objs.findIndex(item => item[field] === data[field]);
};

// 模糊匹配数组中的字符串
export const findSubstrings = (array, query) => {
    // 转义特殊字符以确保正则表达式准确
    const escapedQuery = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    // 创建正则表达式，使用i标志进行不区分大小写的匹配
    const regex = new RegExp(escapedQuery, 'i');
    // 使用filter方法过滤数组，返回匹配正则表达式的字符串
    return array.filter(item => regex.test(item));
};

// ========================= 表格动态高度 =========================
// tableH 距离页面下方的高度 默认270
export const setTableHeight = (tableH = 270) => {
    let tableHeightDetil = window.innerHeight - tableH;
    if (tableHeightDetil <= tableH) {
        return tableH;
    } else {
        return window.innerHeight - tableH;
    };
};